package yb.ecp.fast.infra.infra;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * Created by john on 2017/4/12.
 */
@ControllerAdvice
public class ExceptionAdvice {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ActionResult processExcption(NativeWebRequest request, Exception e) {
        logger.error("这个是一个伟大的异常", e);
        ErrorCode code = ErrorCode.UnExceptedError;


        return new ActionResult(code.getCode(), code.getDesc(), null);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public ActionResult processIllegalArumentExcption(NativeWebRequest request, IllegalArgumentException e) {
        logger.error("这个是一个伟大的异常", e);
        ErrorCode code = ErrorCode.IllegalArument;

        return new ActionResult(code.getCode(), code.getDesc(), null);
    }

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    @ResponseBody
    public ActionResult processSQLIntegrityConstraintViolationException(NativeWebRequest request, IllegalArgumentException e) {
        logger.error("这个是一个伟大的异常", e);
        ErrorCode code = ErrorCode.SQLIntegrityConstraintViolation;
        return new ActionResult(code.getCode(), code.getDesc(), null);
    }
}
